package com.landian.demo.fastJson.demo02;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class test {
    public static void main(String[] args) {
        Map packet = new HashMap();
        Map requestHead = new HashMap();
        Map requestBody = new HashMap();
        Map base = new HashMap();
        List coverageList = new ArrayList();
        Map coverage1 = new HashMap();
        Map coverage2 = new HashMap();
        
        packet.put("requestHead",requestHead );
        packet.put("requestBody",requestBody );
        
        requestHead.put("userId", "userid1234");
        requestHead.put("password", "password1234");
        requestHead.put("serviceNo", "serviceNo1234");
        
        requestBody.put("base", base);
        
        base.put("basekey1", "value1");
        base.put("basekey2", "value2");
        base.put("basekey3", "value3");
        
        requestBody.put("coverage_list", coverageList);
        
        coverageList.add(coverage1);
        coverageList.add(coverage2);

        coverage1.put("coverageCode1", "value1");
        coverage1.put("coverageName1", "value1");
        coverage2.put("coverageCode2", "value2");
        coverage2.put("coverageName2", "value2");
        
        String jsonStr = JSON.toJSONString(packet,true);
        
        System.out.println("把map转换成json String后打印如下:");
        System.out.println(jsonStr);
        
        JSONObject jsonObj = JSON.parseObject(jsonStr);
        
        JSONObject requestHeadObj = jsonObj.getJSONObject("requestBody").getJSONObject("base");
        Set<Entry<String, Object>>  set = requestHeadObj.entrySet();
        System.out.println("jsonObj中requestBody>base节点下的各值如下:");
        for(Entry<String,Object> entry :set){
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
        
    }
}